/*
 * User: Sepheus Angelus
 * Date: 01/03/2008
 * Time: 12:35
 * 
 */

using System;
using System.Collections.Generic;
using System.Collections;

namespace Aphyros_MUD_Server
{
	/// <summary>
	/// Parses out information and distinguishes between 'local' and 'world' commands.
	/// </summary>
	public class CommandParser
	{
		/// <summary>
		/// Parses commands from a user.
		/// </summary>
		/// <param name="Command">The command from the user.</param>
		/// <param name="user">The origin of the command.</param>
		/// <param name="connections">All other users in the World.</param>
		public static void Parse(string Command,Listener user, ArrayList connections)
		{

			Command = Command.Trim();
			//Local Commands
			if(user.Login == false) { LoginHandle.Login(Command, user); }
			if(user.Login == true)
			{
				switch (Command.ToLower())
				{
					case "shutdown":
						if(user.Access == 99)
						{
							Environment.Exit(3);
							Database.Close();
						}
						else
						{
							user.Writer.Write(ANSI.BrightRed);
							user.Writer.WriteLine("Unauthorized Access.");
							user.Writer.Write(ANSI.White);
						}
						break;
					case "sklist":
						if(user.Access == 99)
						{
							try
							{
								ScriptEngine.engine.ExecuteFile("LocalCommands\\SkillList.py");
							}
							catch(Exception e)
							{
								Console.WriteLine(e.ToString());
							}
						}
						break;
					case "clist":
						if(user.Access == 99)
						{
							try
							{
								ScriptEngine.engine.ExecuteFile("LocalCommands\\ClassList.py");
							}
							catch(Exception e)
							{
								Console.WriteLine(e.ToString());
							}
						}
						break;
					case "reload":
						if(user.Access == 99)
						{
							ScriptEngine.Reload();
						}
						break;
					case "north":
						Rooms.Move("North",user);
						break;
					case "south":
						Rooms.Move("South",user);
						break;
					case "east":
						Rooms.Move("East",user);
						break;
					case "west":
						Rooms.Move("West",user);
						break;
					case "look":
						Rooms.Describe(user);
						break;
					case "who":
						foreach(Listener users in connections)
						{
							if(users.Room == user.Room)
							{
								user.Writer.Write(ANSI.BrightMagenta);
								user.Writer.WriteLine("{0} is here.",users.Name);
								user.Writer.Write(ANSI.White);
							}
						}
						break;
					default:
						#region Scripting Engine
						if(Command.Length > 0)
						{
							foreach(Listener users in connections)
							{
								ScriptEngine.engine.AddToPath(Environment.CurrentDirectory + "\\LocalCommands\\");
								ScriptEngine.engine.Globals.Add("Command",Command);
								ScriptEngine.engine.Globals.Add("Player",user);
								ScriptEngine.engine.Globals.Add("Players",users);
								
								try { ScriptEngine.engine.ExecuteFile("LocalCommands\\Commands.py"); }
								catch(Exception e) { Console.WriteLine(e.ToString());}
								
								ScriptEngine.engine.Globals.Remove("Command");
								ScriptEngine.engine.Globals.Remove("Player");
								ScriptEngine.engine.Globals.Remove("Players");
							}
						}
						#endregion
						break;
				}
				if(Command.ToLower().StartsWith("info"))
				{
					string Name = "";
					try { Name = Command.Trim().Substring(5); }
					catch { Name = user.Name; }
					string UserClass = Database.GetClassName(Name);
					user.Writer.WriteLine("{0} is a {1}",Name,UserClass);
					Database.AddSkill(user.UniqueID,Skills.SkillID);
				}
			}
			
			//World Commands
			foreach(Listener users in connections)
			{
				if(user.Login == true)
				{
					#region Externally Scripted
					ScriptEngine.engine.AddToPath(Environment.CurrentDirectory + "\\WorldCommands\\");
					ScriptEngine.engine.Globals.Add("Command",Command);
					ScriptEngine.engine.Globals.Add("Player",user);
					ScriptEngine.engine.Globals.Add("Players",users);
					
					try { ScriptEngine.engine.ExecuteFile("WorldCommands\\Commands.py"); }
					catch(Exception e) { Console.WriteLine(e.ToString());}
					
					ScriptEngine.engine.Globals.Remove("Command");
					ScriptEngine.engine.Globals.Remove("Player");
					ScriptEngine.engine.Globals.Remove("Players");
					#endregion
				}

			}
		}

	}
}
